<template>
    <div>
        <h3>机构添加</h3>
        <table class="table  table-bordered">
            <tbody>
                <tr>
                    <td>机构名称</td>
                    <td>
                        <input type="text" v-model="tian.jgname">
                    </td>
                </tr>
                <tr>
                    <td>机构封面</td>
                    <td>
                        <input type="file" @change="img">
                        <img :src="tian.tu" width="100" height="100" alt="" v-if="tian.tu !=''">
                    </td>
                </tr>
                <tr>
                    <td>特色服务</td>
                    <td>
                        <select v-model="tian.xialaid">
                            <option value="">请选择</option>
                            <option :value="item.id" v-for="item in data1.list">{{ item.xname }}</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>营业时间</td>
                    <td>
                        <input type="date" v-model="tian.yingye">
                    </td>
                </tr>
                <tr>
                    <td>机构地址</td>
                    <td>
                        <input type="text" v-model="tian.dizhi">
                    </td>
                </tr>
                <tr>
                    <td>联系电话</td>
                    <td>
                        <input type="text" v-model="tian.phone">
                    </td>
                </tr>
                <tr>
                    <td>发布状态</td>
                    <td>
                        <input type="radio" name="steat" :value="1" v-model="tian.steat">已发布
                        <input type="radio" name="steat" :value="0" v-model="tian.steat">未发布
                    </td>
                </tr>
                <tr>
                    <td>机构详情</td>
                    <td>
                        <input type="text" v-model="tian.xiangq">
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td>
                        <input type="button" value="修改" class="btn btn-primary" @click="ok">
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
</template>

<script setup lang="ts">
import {ref,reactive, onMounted} from 'vue'
import axios from 'axios';
import {useRoute,useRouter} from 'vue-router'
const route=useRoute();
const router=useRouter();
let tian:any=reactive({
    id:route.query.id,
    jgname: '',
    tu: '',
    xialaid: '',
    yingye: '',
    dizhi: '',
    phone: '',
    steat: '',
    xiangq: '',
})

const img=(e:any)=>{
    let obj=e.target.files[0];
    let f=new FormData();
    f.append('file',obj);
    axios({
        url:'/api/img/tu',
        method:'post',
        data:f
    })
    .then((res)=>{
        console.log(res);
        tian.tu=res.data;
    })
    .catch((err)=>{
        console.log(err);
    })
}

const data1:any=reactive({
    list:[]
})
onMounted(()=>{
    getlist1();
    fantians();
})

const getlist1=()=>{
    axios({
        url:'/api/jg/xshow',
        method:'get'
    })
    .then((res)=>{
        console.log(res);
        data1.list=res.data;
    })
    .catch((err)=>{
        console.log(err);
    })
}
const fantians=()=>{
    axios({
        url:'/api/jg/fantian',
        method:'get',
        params:{
            id:route.query.id
        }
    })
    .then((res)=>{
        console.log(res);
        tian.jgname=res.data.jgname
        tian.tu=res.data.tu
        tian.xialaid=res.data.xialaid
        tian.yingye=res.data.yingye
        tian.dizhi=res.data.dizhi
        tian.phone=res.data.phone
        tian.steat=res.data.steat
        tian.xiangq=res.data.xiangq
    })
    .catch((err)=>{
        console.log(err);
    })
}
const ok=()=>{
    axios({
        url:'/api/jg/xiugai',
        method:'put',
        data:tian
    })
    .then((res)=>{
        console.log(res);
        if(res.data==-1)
    {
        alert('名称不能重复');
        return;
    }
       else if(res.data>0)
    {
        alert('修改成功');
        router.push('/Jshow');
    }
    else{
        alert('修改失败');
    }
    })

}
</script>

<style scoped>

</style>